<?php
namespace Admin\Controller;

class TeacherController extends BaseController {

    public function index() {

        $Teacher = D ('Teacher');

        $map['department_id'] = $this->admin['department_id'];

        $limit = \Api\Libs\PageUtil::getLimit(10);
        $page = \Api\Libs\PageUtil::getPageHtml($Teacher->getCountByWhere($map),10);

        $teacherList = $Teacher->getTeacherByWhere($map,$limit);

        $this->assign('teacherList',$teacherList);
        $this->assign('page',$page);
        $this->display();

    }



    public function add() {

        if (!IS_POST) {
            $this->display();
        } else {

            $data['teacher_id'] = I ('request.teacher_id','');
            $data['department_id'] = $this->admin['department_id'];
            $data['password'] = md5('123456');
            $data['name'] = I ('request.name','');
            $data['sex'] = I ('request.sex','');
            $data['zc'] = I ('request.zc','');
            $data['email'] = I ('request.email');
            $data['telphone'] = I ('request.telphone');

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Teacher = D ('Teacher');
            $flag = $Teacher->_add($data);

            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }

    public function edit() {
        if (!IS_POST) {
            $id = I ('request.id','');
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Teacher = D ('Teacher');
            $t = $Teacher->getTeacherById($id) ;
            if (empty($t)) $this->error('教师不存在');
            // 确定修改的时当前院系的老师
            if ($t['department_id'] !== $this->admin['department_id']) $this->error('非法访问');

            $this->assign('teacher',$t);
            $this->display();
        } else {

            $data['id'] = I ('request.id','');
            $data['name'] = I ('request.name','');
            $data['sex'] = I ('request.sex','');
            $data['zc'] = I ('request.zc','');
            $data['email'] = I ('request.email','');
            $data['telphone'] = I ('request.telphone','');
            $data['password'] = md5(I ('request.password',''));

            if ($data['password'] === md5('default')) unset($data['password']);
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Teacher = D ('Teacher');
            $flag = $Teacher->_edit($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        }
    }


    public function del() {
        if (!IS_POST) $this->error('非法访问');

        $id = I ('request.id','');
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $Teacher = D ('Teacher');
        $flag = $Teacher->_del($id);
        if ($flag === '000000')
            $this->out('RESULT_OK','',__CONTROLLER__);
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
    }

}